﻿using ExChange.Model;
using System.Web.Mvc;
using System.Linq;
using System.Data.Entity;

namespace ExChange.Web.Controllers
{
    public class SettingController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {

            return View();
        }


        public ActionResult TypeEnumList()
        {
            var typeList = EnumHelper.EnumToList<EnumHelper.UploadType>();
            return Json(typeList, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetList(int page, int rows)
        {
            var list = entity.Setting.ToList();
            var totalcount = list.Count();
            var data = list.OrderByDescending(f => f.ID).Skip((page - 1) * rows).Take(rows).Select(f => new
            {

                f.ID,
                f.Url,
                f.Type,
                TypeText = f.Type != null ? ((EnumHelper.UploadType)f.Type).ToString() : "暂无",
            });
            return Json(new { rows = data, total = totalcount }, JsonRequestBehavior.AllowGet);
        }
        [ValidateInput(false)]
        public JsonResult Save(Setting model)
        {
            //string pathList = Request["pathList"]; 
            if (model.ID == 0)
            {
                entity.Setting.Add(model);
            }
            else
            {
                //var setting = entity.Setting.FirstOrDefault(f => f.ID == model.ID);
                //setting.Url = model.Url;
                entity.Entry<Setting>(model).State = EntityState.Modified;
            }
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }
    }
}
